package com.dfqy.service;

import com.dfqy.dao.UserDao;
import com.dfqy.pojo.User;
import com.dfqy.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Author : atyushu
 * Package : com.dfqy.service
 * ClassName : UserService
 * Create : 2023/7/31 - 13:19
 * Version : v1.0
 * Description : The class of service for user
 */
@Service
@Cacheable(keyGenerator = "keyg", cacheNames = "user")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
public class UserService {

    @Autowired
    private UserDao userDao;

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int insert(User user) {
        return userDao.insert(user);
    }

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int delete(long... id) {
        return userDao.delete(id);
    }

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int update(User user) {
        return userDao.update(user);
    }

    public User getById(long id) {
        return userDao.getById(id);
    }

    public Page<User> page(int page, int limit, String uname, String phone, Date regtimeStart, Date regtimeEnd, String qq, String weibo) {
        return new Page<>(userDao.page(page, limit, uname, phone, regtimeStart, regtimeEnd, qq, weibo), page, userDao.countByLike(uname, phone, regtimeStart, regtimeEnd, qq, weibo), limit);
    }

    public User signIn(String uname, String pass) {
        return userDao.signIn(uname, pass);
    }

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int ban(long id) {
        return userDao.ban(id);
    }

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int restore(long id) {
        return userDao.restore(id);
    }

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int signUp(User user) {
        return userDao.signUp(user);
    }

    public int checkUname(String uname) {
        return userDao.checkUname(uname);
    }

    public int checkPhone(String phone) {
        return userDao.checkPhone(phone);
    }

    @CacheEvict(allEntries = true, cacheNames = "user")
    public int reloadPass(long id, String pass) {
        return userDao.reloadPass(id, pass);
    }
}
